//
//  NetHelper.m
//  Test
//
//  Created by sdadmin on 2018/6/8.
//  Copyright © 2018年 sdadmin. All rights reserved.
//

#import "NetHelper.h"

#import <PPNetworkHelper/PPNetworkHelper.h>
#import <AFNetworking.h>

static AFHTTPSessionManager * manager = nil;

@implementation NetHelper

+(AFHTTPSessionManager *)managerWithTimeOut:(float)timeOut{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [AFHTTPSessionManager manager];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        //     AFN会JSON解析返回的数据
        manager.responseSerializer = [AFJSONResponseSerializer serializer];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"application/xml",@"text/xml",@"text/html",@"multipart/form-data",@"application/x-www-form-urlencoded",nil];
        manager.requestSerializer.timeoutInterval = timeOut;
    });
    return manager;
}
#pragma mark 无缓存Post请求
+(NSURLSessionTask *)requestNoCachePostWithUrlStr:(NSString *)urlStr params:(NSMutableDictionary *)params successBlock:(NetRequestSuccess)successBlock failBlock:(NetRequestFail)failBlock{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    return [PPNetworkHelper POST:urlStr parameters:params success:^(id responseObject) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        successBlock(responseObject);
    } failure:^(NSError *error) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        failBlock(error);
    }];
}
#pragma mark 带缓存Post请求
+(NSURLSessionTask *)requestCachePostWithUrlStr:(NSString *)urlStr params:(NSMutableDictionary *)params cacheBlock:(void(^)(id cacheData))cacheBlock successBlock:(NetRequestSuccess)successBlock failBlock:(NetRequestFail)failBlock{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    return [PPNetworkHelper POST:urlStr parameters:params responseCache:^(id responseCache) {
        cacheBlock(responseCache);
    } success:^(id responseObject) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        successBlock(responseObject);
    } failure:^(NSError *error) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        failBlock(error);
    }];
}

#pragma mark AF post网络请求
+ (void)requestWithUrlStr:(NSString *)urlStr
         andParameters:(NSDictionary *)parameters
        completedBlock:(NetRequestSuccess)successblock
          failuerBlock:(NetRequestFail)failblock
{
    AFHTTPSessionManager *manager = [self managerWithTimeOut:30];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    [manager POST:urlStr parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        successblock(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        failblock(error);
    }];
    
}

@end
